package com.ruoyi.system.apiController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.YyAdmin;
import com.ruoyi.system.service.IYyAdminService;
import com.ruoyi.system.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class YyAdminApiController {

    @Autowired
    private IYyAdminService yyAdminService;

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public AjaxResult list(YyAdmin yyAdmin) {
        if (yyAdmin == null){
            yyAdmin = new YyAdmin();
        }
        List<YyAdmin> yyAdmins = yyAdminService.selectYyAdminList(yyAdmin);
        return AjaxResult.success(yyAdmins);
    }

    /**
     * 添加管理员
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("name") String name,
                          @RequestParam("avatar") MultipartFile avatar,
                          @RequestParam("role") String role,
                          @RequestParam("phone") String phone,
                          @RequestParam("email") String email) {
        try {
            YyAdmin admin = new YyAdmin();
            if (avatar != null){
                String photoFileName = FileUtil.savePhoto(avatar);
                admin.setAvatar(photoFileName);
            }
            admin.setUsername(username);
            admin.setPassword(password);
            admin.setName(name);
            admin.setRole(role);
            admin.setPhone(phone);
            admin.setEmail(email);
            int i = yyAdminService.insertYyAdmin(admin);
            if (i > 0) {
                return AjaxResult.success("添加成功");
            } else {
                return AjaxResult.error("添加失败");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改管理员
     * @param
     * @return
     */
    @PutMapping("/update")
    public AjaxResult update(
                            @RequestParam(name = "id", required = false) String id,
                            @RequestParam(name = "username", required = false) String username,
                             @RequestParam(name = "password", required = false) String password,
                             @RequestParam(name = "name", required = false) String name,
                             @RequestParam(name = "avatar", required = false) MultipartFile avatar,
                             @RequestParam(name = "role", required = false) String role,
                             @RequestParam(name = "phone", required = false) String phone,
                             @RequestParam(name = "email", required = false) String email) {
        try {
            YyAdmin admin = new YyAdmin();
            if (avatar != null){
                String photoFileName = FileUtil.savePhoto(avatar);
                admin.setAvatar(photoFileName);
            }
            admin.setUsername(username);
            admin.setPassword(password);
            admin.setName(name);
            admin.setRole(role);
            admin.setPhone(phone);
            admin.setEmail(email);
            admin.setId(Long.parseLong(id));
            int i = yyAdminService.updateYyAdmin(admin);
            if (i > 0) {
                return AjaxResult.success("修改成功");
            } else {
                return AjaxResult.error("修改失败");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 删除管理员
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        int i = yyAdminService.deleteYyAdminById(id);
        if (i > 0) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }


}
